﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;
using System.Data.Common;
using System.Data;

namespace DAL.BusinessDeal
{
   public class StVesselBerthDAL
    {
       private const string selectAllTerminal = null;


        /// <summary>
        /// Get DataTable
        /// </summary>
        /// <param name="textWhere">conditon</param>
        /// <param name="orderText">sort</param>
        /// <returns></returns>
        public DataTable GetTable()
        {
            return GetDataTable(selectAllTerminal);
        }




       /// <summary>
       /// 新增靠泊记录
       /// </summary>
       /// <param name="P_Operation_Type"></param>
       /// <param name="p_SCD_ID"></param>
       /// <param name="P_VBT_PBTHDT"></param>
       /// <param name="P_VBT_PDPTDT"></param>
       /// <param name="P_VBT_PBTHDRAFT"></param>
       /// <param name="P_VBT_PBTHDRAFT_P"></param>
       /// <param name="P_VBT_PBTHDRAFT_A"></param>
       /// <param name="P_VBT_PDPTDRAFT"></param>
       /// <param name="P_VBT_PDPTDRAFT_P"></param>
       /// <param name="vVBT_PDPTDRAFT_A"></param>
       /// <returns></returns>
        public string InsertUPdateToTable(int P_Operation_Type, string p_SCD_ID, DateTime P_VBT_PBTHDT, DateTime P_VBT_PDPTDT,
                   string P_VBT_PBTHDRAFT, string P_VBT_PBTHDRAFT_P, string P_VBT_PBTHDRAFT_A, string P_VBT_PDPTDRAFT
                 , string P_VBT_PDPTDRAFT_P, string vVBT_PDPTDRAFT_A, string P_VBT_TER_ID, string P_USER_ID, string P_VBT_ID, string P_VBT_LDUNLDFG)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetStoredProcCommand("web_sp_process_vesselberth");
            db.AddInParameter(dbCommand, "P_Operation_Type", DbType.Int32, P_Operation_Type);
            db.AddInParameter(dbCommand, "P_SCD_ID", DbType.Int32, p_SCD_ID);
            db.AddInParameter(dbCommand, "P_VBT_PBTHDT", DbType.DateTime, P_VBT_PBTHDT);
            db.AddInParameter(dbCommand, "P_VBT_PDPTDT", DbType.DateTime, P_VBT_PDPTDT);
            db.AddInParameter(dbCommand, "P_VBT_PBTHDRAFT", DbType.Int32, string.IsNullOrEmpty(P_VBT_PBTHDRAFT) ? 0 : int.Parse( P_VBT_PBTHDRAFT));
            db.AddInParameter(dbCommand, "P_VBT_PBTHDRAFT_P", DbType.Int32, string.IsNullOrEmpty(P_VBT_PBTHDRAFT_P) ? 0 : int.Parse(P_VBT_PBTHDRAFT_P));
            db.AddInParameter(dbCommand, "P_VBT_PBTHDRAFT_A", DbType.Int32, string.IsNullOrEmpty(P_VBT_PBTHDRAFT_A) ? 0 : int.Parse(P_VBT_PBTHDRAFT_A));
            db.AddInParameter(dbCommand, "P_VBT_PDPTDRAFT", DbType.Int32, string.IsNullOrEmpty(P_VBT_PDPTDRAFT) ? 0 : int.Parse(P_VBT_PDPTDRAFT));
            db.AddInParameter(dbCommand, "P_VBT_PDPTDRAFT_P", DbType.Int32, string.IsNullOrEmpty(P_VBT_PDPTDRAFT_P) ? 0 : int.Parse(P_VBT_PDPTDRAFT_P));
            db.AddInParameter(dbCommand, "P_VBT_PDPTDRAFT_A", DbType.Int32, string.IsNullOrEmpty(vVBT_PDPTDRAFT_A) ? 0 : int.Parse(vVBT_PDPTDRAFT_A));
            db.AddInParameter(dbCommand, "P_VBT_TER_ID", DbType.Int32, string.IsNullOrEmpty(P_VBT_TER_ID) ? 0 : int.Parse(P_VBT_TER_ID));
            db.AddInParameter(dbCommand, "P_USER_ID", DbType.Int32, string.IsNullOrEmpty(P_USER_ID) ? 0 : int.Parse(P_USER_ID));
            db.AddInParameter(dbCommand, "P_VBT_ID", DbType.Int32, string.IsNullOrEmpty(P_VBT_ID) ? 0 : int.Parse(P_VBT_ID));
            db.AddInParameter(dbCommand, "P_VBT_LDUNLDFG", DbType.String, P_VBT_LDUNLDFG);

            db.AddOutParameter(dbCommand, "ReturnValue", DbType.String, 400);

            dbCommand.Parameters["ReturnValue"].Direction = ParameterDirection.Output;
            db.ExecuteNonQuery(dbCommand);
            return dbCommand.Parameters["ReturnValue"].Value.ToString();
        }

        /// <summary>
        /// Get DataTable
        /// </summary>
        /// <param name="sql">SQL statement</param>
        /// <returns></returns>
        private DataTable GetDataTable(string sql)
        {
            Database db = DatabaseFactory.CreateDatabase();
            DbCommand dbCommand = db.GetSqlStringCommand(sql);
            DataTable dt = db.ExecuteDataTable(dbCommand);
            if (dt != null)
            {
                dt.TableName = "ST_Terminal";
            }
            return dt;
        }
    }
}
